﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace VSTalk.Engine
{
    public static class AppServices 
    {
        private static readonly IDictionary<Type, object> _container = new ConcurrentDictionary<Type, object>();

        public static void Set<T>(object service)
        {
            if (service is T)
            {
                _container.Add(typeof(T), service);
            }
            else
            {
                throw new ArgumentException("Unable to set service", "service");
            }
        }

        public static T Get<T>()
        {
            return (T) _container[typeof(T)];
        }

        public static void Dispose()
        {
            foreach (var o in _container.Values)
            {
                var service = o as IDisposable;
                if (service != null)
                {
                    service.Dispose();
                }
            }
        }
    }
}